﻿using MyToDo.Extensions;
using MyToDo.ViewModels;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyToDo.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainView : Window
    {
        private readonly MainViewModel _mainVm;
        private readonly IRegionManager _regionManager;

        /// <summary>
        /// 构造函数注入MainViewModel类,在Prism框架实例化MainView的时候,会自动从容器中
        /// 获取MainViewModel的实例对象.然后MainViewModel被我们注册为了单例的模式,
        /// 所以获取的是同一个MainViweModel实例,这里主要是更改窗口的最大化和最小化的时候的状态
        /// 属性注入也可以,属性注入的方式就是在属性的上面添加上[Dependency]特性,但是不推荐使用属性注入,
        /// 使用构造注入的好处就是,你可以确保在构造函数完成之后,所有的依赖项都已经被正确的初始化和赋值.如果使用属性
        /// 注入,虽然在编码上看起来更加的简洁,但是你无法保证在构造函数执行完毕后,所有的依赖项都已经被赋值,增加了出错的可能性.
        /// 所以我们推荐在使用IOC容器的时候,优先选择使用构造函数的注入方式
        /// </summary>
        /// <param name="MainVm"></param>
        public MainView(MainViewModel MainVm, IRegionManager RegionManager)
        {
            InitializeComponent();
            _mainVm = MainVm;
            WindowState = WindowState.Normal;
            _mainVm.AppMaxmizeState = "Normal";

            _regionManager = RegionManager;
            Loaded += MainView_OnLoaded;
        }

        private void MainView_OnLoaded(object sender, RoutedEventArgs e)
        {
            _regionManager.Regions[PrismManager.MainViewRegion].RequestNavigate(nameof(IndexView));
        }

        private void MenuToggleButton_OnClick(object sender, RoutedEventArgs e)
        {

        }

        // 最小化
        private void BtnMinus_OnClick(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void BtnMaxmize_OnClick(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
                _mainVm.AppMaxmizeState = "Max";
            }
            else
            {
                WindowState = WindowState.Normal;
                _mainVm.AppMaxmizeState = "Normal";
            }
        }

        private void BtnClose_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        // 鼠标拖动
        private void MouseLeftButtonDown_OnClick(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        // 导航栏双击放大和缩小
        private void MouseDouble_OnClick(object sender, MouseButtonEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
                _mainVm.AppMaxmizeState = "Max";
            }
            else
            {
                WindowState = WindowState.Normal;
                _mainVm.AppMaxmizeState = "Normal";
            }
        }

        // 选项发生改变的时候触发的实践,主要用来将drawerHost实现自动关闭
        private void Selection_OnChange(object sender, SelectionChangedEventArgs e)
        {
            drawerHost.IsLeftDrawerOpen = false;
        }

        private void BtnGoForwardMouseDouble_OnClick(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void BtnGoBackMouseDouble_OnClick(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }
}
